`
https://leetcode.cn/problems/add-one-row-to-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} val
 * @param {number} depth
 * @return {TreeNode}
 */
var addOneRow = function (root, val, depth) {
  // 如果是添加到第一行，需要特殊处理
  if (depth === 1) {
    const newRoot = new TreeNode(val)
    newRoot.left = root
    return newRoot
  }

  const traverse = (root, dep) => {
    if (root === null) return

    // 到达指定深度的上一层
    if (dep === depth - 1) {
      // 插入新结点
      const leftNode = new TreeNode(val)
      const rightNode = new TreeNode(val)
      leftNode.left = root.left
      rightNode.right = root.right
      root.left = leftNode
      root.right = rightNode
    } else {
      traverse(root.left, dep + 1)
      traverse(root.right, dep + 1)
    }
  }

  traverse(root, 1)

  return root
};